package com.leyou.item.controller;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.*;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import com.sun.org.apache.regexp.internal.RE;
import jdk.net.SocketFlow;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: SGS
 * @Date : 2020/11/17 16:46
 * @Version :
 */
@RestController
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private SpuService spuService;

    @Resource
    private SkuService skuService;

    @Resource
    private SpuDetailService spuDetailService;

    @Resource
    private SpecParamService specParamService;
    /**
     * 分页查询spu
     * @param reqSpu 查询条件
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(ReqSpuDTO reqSpu){
        return ResponseEntity.ok( spuService.querySpuByPage(reqSpu));
    }

    /**
     * 添加商品
     * @param spuDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改spu
     * @param spuDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询sku集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    /**
     * 根据spuId查询sku集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(SkuDTO.convertEntityList(
                skuService.query().eq("spu_id",id).list()));
    }

    /**
     * 根据id查询商品的详情Detail
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailById(@RequestParam("id") Long id){
        SpuDetail detail = spuDetailService.getById(id);
        return ResponseEntity.ok(new SpuDetailDTO(detail));
    }

    /**
     * spuId查询所有规格参数
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> queryParamBySpuId(@RequestParam("id") Long id,
                                                          @RequestParam(value = "searching", required = false) Boolean searching){
        return ResponseEntity.ok(spuDetailService.queryParamBySpuId(id,searching));
    }

    /**
     * 修改商品上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long id,
                                                  @RequestParam(value = "saleable", required = false) Boolean saleable){
    spuService.updateSpuSaleable(id,saleable);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询商品以及规格参数
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuSkuDetailById(@PathVariable("id") Long id){
        return ResponseEntity.ok(spuService.querySpuSkuDetailById(id));
    }
    /**
     * 根据id查询商品spu
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){

        return ResponseEntity.ok( spuService.getById(id));
    }

    /**
     * 批量减库存
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> updateStockById(@RequestBody HashMap<Long,Integer> map){
        skuService.updateStockById(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}

